import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
// import axios from 'axios'
import md5 from 'js-md5'
import BaiduMap from 'vue-baidu-map'
import filter from './filters/filter'

import locale from "element-ui/lib/locale/lang/en"


// 导入全局样式表
import '../src/assets/css/global.css'

import echarts from 'echarts'
Vue.prototype.$echarts = echarts

// 引入字体图标
import './assets/iconfont/iconfont.css'

// 全局引入elementUI
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// Vue.use(ElementUI)
Vue.use(ElementUI, {locale})
// 将md5加到原型链中
Vue.prototype.$md5 = md5

// 将百度地图注册到Vue中
Vue.use(BaiduMap, {
  ak: 'sgFdbtEFGgtarT9q6cj4V2aznPsP7Ukt'
})

//全局导入过滤器
Object.keys(filter).forEach(key => Vue.filter(key, filter[key]))

// 封装axios中设置过期token移除,在钩子中增加判断token的存在,不存在则直接跳转登录页面
router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem('token')
  if (token) {
    // 如果用户信息存在则往下执行。
    next()
  } else {
    // 如果用户token不存在则跳转到login页面
    if (to.path === '/login') {
      next()
    } else {
      if(window.location.href){
        sessionStorage.setItem('href', window.location.href)
      }
      next({path:'/login'})
    }
  }
})

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
